package cn.smbms.service.provider;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import cn.smbms.mapper.bill.BillMapper;
import cn.smbms.mapper.provider.ProviderMapper;
import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.tools.MybatisUtil;
@Service
public class ProviderServiceImpl implements ProviderService {
	@Resource
	private ProviderMapper providerMapper;
	
	@Override
	public boolean add(Provider provider) {
		
		return providerMapper.add(provider)>0?true:false;
	}

	@Override
	public List<Provider> getProviderList(String proName,String proCode) {
		// TODO Auto-generated method stub
		
		return providerMapper.getProviderList(proName, proCode);
	}

	
	@Override
	public int deleteProviderById(String delId) {
		// TODO Auto-generated method stub
		SqlSession sqlSession=null;
		ProviderMapper providerMapper=null;
		BillMapper billMapper=null;
		int billCount = -1;
		try {
			sqlSession =MybatisUtil.getSqlsession();
			providerMapper=sqlSession.getMapper(ProviderMapper.class);
			billCount = billMapper.getBillCountByProviderId(delId);
			if(billCount == 0){
				providerMapper.deleteProviderById( delId);
			}
			sqlSession.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			billCount = -1;
			sqlSession.rollback();
			
		}finally{
			MybatisUtil.CloseSqlsession();
		}
		return billCount;
	}

	@Override
	public Provider getProviderById(String id) {
		// TODO Auto-generated method stub
		Provider provider = null;
		SqlSession sqlSession=null;
		ProviderMapper providerMapper=null;
		try{
			sqlSession =MybatisUtil.getSqlsession();
			providerMapper=sqlSession.getMapper(ProviderMapper.class);
			provider = providerMapper.getProviderById(id);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			provider = null;
		}finally{
			MybatisUtil.CloseSqlsession();
		}
		return provider;
	}

	@Override
	public boolean modify(Provider provider) {
		// TODO Auto-generated method stub
		SqlSession sqlSession=null;
		ProviderMapper providerMapper=null;
		boolean flag = false;
		try {
			sqlSession =MybatisUtil.getSqlsession();
			providerMapper=sqlSession.getMapper(ProviderMapper.class);
			if(providerMapper.modify(provider) > 0)
				flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			MybatisUtil.CloseSqlsession();
		}
		return flag;
	}

}
